package com.three.iot.rest.controller.common;

import com.three.iot.api.UserDeviceService;
import com.three.iot.commons.constant.CommonConstants;
import com.three.iot.commons.enums.Result;
import com.three.iot.commons.enums.ResultCode;
import com.three.iot.commons.dto.DevicePropertyControlReqDto;
import com.three.iot.commons.dto.DevicePropertyDataReqDto;
import com.three.iot.rest.utils.RequestUtils;
import com.three.iot.rest.vo.api.DevicePropertyControlReqVo;
import com.three.iot.rest.vo.api.DevicePropertyDataReqVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Copyright (C), 2019-2019, 三人行工作室
 * FileName: AbstractDeviceAttrController
 * Author:   luiz
 * Date:     2019/6/15 11:59
 * Description: 设备属性公共部分
 * History:
 * <author>          <time>          <version>          <desc>
 * luiz            2019/6/15 11:59       版本号              描述
 */
@Component
@Slf4j
public class AbstractDeviceAttrController {
    @Resource
    UserDeviceService userDeviceService;
    private static final String LOG_HEAD="【设备属性】";
    /**
     * 获取设备某个属性数据详情
     */
    @ApiOperation("获取设备某个属性数据详情")
    @PostMapping("/findSigleAttrDataList")
    private Result findSigleAttrDataList(@RequestBody DevicePropertyDataReqVo devicePropertyDataReqVo, HttpServletRequest request) {
        try{
            String userNo= RequestUtils.getCurrentUserNo(request);
            DevicePropertyDataReqDto devicePropertyDataReqDto=new DevicePropertyDataReqDto();
            BeanUtils.copyProperties(devicePropertyDataReqVo,devicePropertyDataReqDto);
            devicePropertyDataReqDto.setIdentifier(devicePropertyDataReqVo.getAttrNo());
            Long endTime=System.currentTimeMillis();
            Long startTime=endTime;
            if("0".equals(devicePropertyDataReqVo.getShowType())){
                int showHours=devicePropertyDataReqVo.getShowHours()==null? CommonConstants.DEFAULT_SHOW_HOURS:Integer.valueOf(devicePropertyDataReqVo.getShowHours());
                if(devicePropertyDataReqVo.getStartTime()==null){
                    long diffTime=showHours*CommonConstants.ONE_HOUR_MILSECONDS;
                    startTime=endTime-diffTime;
                }else {
                    startTime= Long.valueOf(devicePropertyDataReqVo.getStartTime());
                }
                devicePropertyDataReqDto.setStartTime(startTime);
                devicePropertyDataReqDto.setEndTime(endTime);
            }else {
                if(devicePropertyDataReqVo.getStartTime()==null){
                    return Result.newError(ResultCode.COMMON_PARAM_INVALID.getCode(),"开始时间不能为空!");
                }
                if(devicePropertyDataReqVo.getEndTime()==null){
                    return Result.newError(ResultCode.COMMON_PARAM_INVALID.getCode(),"结束时间不能为空!");
                }
                startTime= Long.valueOf(devicePropertyDataReqVo.getStartTime());
                endTime=Long.valueOf(devicePropertyDataReqVo.getEndTime());
            }
           devicePropertyDataReqDto.setStartTime(Long.valueOf(startTime));
           devicePropertyDataReqDto.setEndTime(Long.valueOf(endTime));
            int asc=devicePropertyDataReqVo.getAsc()==null?CommonConstants.DEFAULT_PAGE_ASC:Integer.valueOf(devicePropertyDataReqVo.getAsc());
            devicePropertyDataReqDto.setAsc(asc);
            int pageSize=devicePropertyDataReqVo.getPageSize()==null?CommonConstants.DEFAULT_PAGE_SIZE:Integer.valueOf(devicePropertyDataReqVo.getPageSize());
            devicePropertyDataReqDto.setPageSize(pageSize);
            devicePropertyDataReqDto.setUserNo(userNo);
            log.info(LOG_HEAD+"获取设备某个属性数据详情:{}",devicePropertyDataReqDto);
            return userDeviceService.findSigleAttrDataList(devicePropertyDataReqDto);
        }catch (Exception e){
            log.error(LOG_HEAD+"获取设备某个属性数据详情发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


    /**
     * 设置设备属性
     */
    @ApiOperation("设置设备属性")
    @PostMapping("/setDeviceProperty")
    private Result setDeviceProperty(@RequestBody DevicePropertyControlReqVo devicePropertyControlVo, HttpServletRequest request) {
        try{
            String userNo= RequestUtils.getCurrentUserNo(request);
            log.info(LOG_HEAD+"用户:{}设置设备属性:{}",userNo,devicePropertyControlVo);
            DevicePropertyControlReqDto devicePropertyControlReqDto=new DevicePropertyControlReqDto();
            BeanUtils.copyProperties(devicePropertyControlVo,devicePropertyControlReqDto);
            return userDeviceService.setDeviceProperty(devicePropertyControlReqDto);
        }catch (Exception e){
            log.error(LOG_HEAD+"设置设备属性发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
}
